package com.somlsis.activity

import android.content.Intent
import android.view.LayoutInflater
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.base.AppConfig
import com.base.BaseActivity
import com.clj.fastble.data.BleDevice
import com.net.HttpResponse
import com.somlsis.R
import com.somlsis.bean.User
import com.somlsis.databinding.ActivityBasicInfoBinding
import com.somlsis.fragment.BirthdayFragment
import com.somlsis.fragment.BodyInfoFragment
import com.somlsis.fragment.BasicInfoDeviceFragment
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class BasicInfoActivity : BaseActivity<ActivityBasicInfoBinding>() {
    private val fragmentManager: FragmentManager = supportFragmentManager
    private var transaction: FragmentTransaction? = null
    private lateinit var birthdayFragment: BirthdayFragment
    private lateinit var bodyInfoFragment: BodyInfoFragment
    private lateinit var deviceFragment: BasicInfoDeviceFragment
    private var birthday: String? = null
    private var mSex: Int = -1
    private var user = dataCenter.getUser()
    private lateinit var launcher: ActivityResultLauncher<Intent>
    override fun getViewBinding(): ActivityBasicInfoBinding {
        return ActivityBasicInfoBinding.inflate(LayoutInflater.from(this))
    }

    override fun createView() {
        val user = dataCenter.getUser()
        birthdayFragment = BirthdayFragment(object : OnBasicInfoNextClickListener {


            override fun onBirthdayNextClick(date: String, sex: Int) {
                birthday = date
                mSex = sex
                bodyInfoFragment?.let {
                    replaceFragment(bodyInfoFragment)
                }

            }


        })
        bodyInfoFragment = BodyInfoFragment(object : OnBasicInfoNextClickListener {


            override fun onBodyInfoNextClick(weight: String, height: String) {
                user?.let {
                    birthday?.let { bir ->
                        dataCenter.editUser(
                            "user" + it.unique,
                            it.phone,
                            bir,
                            mSex.toString(),
                            height,
                            weight,
                            object : Callback<HttpResponse<User>> {
                                override fun onResponse(
                                    call: Call<HttpResponse<User>>,
                                    response: Response<HttpResponse<User>>
                                ) {
                                    if (response.code() == AppConfig.SUCCEED_CODE) {
                                        val httpBody = response.body()
                                        httpBody?.let { body ->
                                            dataCenter.setUser(body.data)
                                            replaceFragment(deviceFragment)
                                        }
                                    }
                                }

                                override fun onFailure(
                                    call: Call<HttpResponse<User>>,
                                    t: Throwable
                                ) {

                                }
                            })
                    }

                }


            }


        })
        deviceFragment = BasicInfoDeviceFragment(object : OnBasicInfoNextClickListener {
            override fun onDeviceAddOrConnectClick(bleDevice: BleDevice?) {
                super.onDeviceAddOrConnectClick(bleDevice)
                if (bleDevice == null) {
                    launcher.launch(Intent(this@BasicInfoActivity, AddDeviceActivity::class.java))
                } else {

                }
            }
        })

        replaceFragment(birthdayFragment)
        launcher = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) { result ->
            //处理返回的结果
            val code = result.resultCode //返回码 如：Activity.RESULT_OK、Activity.RESULT_CANCELED
            val data = result.data
            data?.let {
                val deviceMac = it.getStringExtra("device_mac")
                val intent = Intent(this, HomeActivity::class.java)
                intent.putExtra("device_mac", deviceMac)
                startActivity(intent)
                finish()
            }


        }
    }

    private fun replaceFragment(fragment: Fragment) {

        transaction = fragmentManager.beginTransaction()
        transaction?.let {
            it.replace(R.id.basic_info_fl, fragment).commit()
        }
    }

    interface OnBasicInfoNextClickListener {
        fun onBirthdayNextClick(date: String, sex: Int) {}
        fun onBodyInfoNextClick(weight: String, height: String) {}
        fun onDeviceAddOrConnectClick(bleDevice: BleDevice?) {}
    }
}